# vim:set ft= ts=4 sw=4 et fdm=marker:

use Test::Nginx::Socket::Lua;

plan tests => repeat_each() * (blocks() * 3);

$ENV{TEST_NGINX_TIMESTAMP}        = 1590502289;
$ENV{TEST_NGINX_TIMESTAMP_MS}     = 1590502289.234;

our $http_config = <<'_EOC_';
    lua_package_path "${prefix}../../script/?.ljbc;${prefix}../../script/?.lua;;";

    root "";

    init_by_lua_block {
        local config = require("config")

        -- re-define the current timestamp
        ngx.time = function() return $TEST_NGINX_TIMESTAMP    end;
        ngx.now  = function() return $TEST_NGINX_TIMESTAMP_MS end;
    }
_EOC_


no_long_string();

run_tests();

__DATA__


=== TEST 1: jwt encode
--- http_config eval: $::http_config
--- config
    location = /t {
        content_by_lua_block {
            local jwt    = require "shared.auth.jwt"

            local timestamp = ngx.time()

            local token, err = jwt.pack({
                username    = "luffy",
                roles       = {
                    "admin",
                    "custome_service",
                },
                permissions = {
                    "dashboard/user_management.%:rw",
                    "dashboard/role_management.%:rw",
                    "dashboard/game_management.%:rw",
                },
                ip  = "55.11.22.33",
                ttl = 500,
                exp = timestamp + 500,
            })
            if err then
                ngx.log(ngx.ERR, err)
            end

            -- perform response
            do
                ngx.header.content_type = 'text/plain'
                ngx.print(token)
            end
        }
    }
--- request
GET /t
--- response_body_like: ^[\-\._A-Za-z0-9]{375}$
--- error_code: 200
--- no_error_log
[error]



=== TEST 2: jwt decode
--- http_config eval: $::http_config
--- config
    location = /t {
        content_by_lua_block {
            local ljson  = require "shared.test.ljson"
            local jwt    = require "shared.auth.jwt"

            local TOKEN
            do
                ngx.req.read_body()
                TOKEN = ngx.req.get_body_data()
            end

            local model, err = jwt.unpack(TOKEN)
            if err then
                ngx.log(ngx.ERR, err)
            end

            -- perform response
            do
                ngx.header.content_type = 'application/json; charset=utf-8'
                ngx.print(ljson.encode(model))
            end
        }
    }
--- request
GET /t
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1OTA1MDI3ODksInVzZXJuYW1lIjoibHVmZnkiLCJpcCI6IjU1LjExLjIyLjMzIiwicm9sZXMiOlsiYWRtaW4iLCJjdXN0b21lX3NlcnZpY2UiXSwidHRsIjo1MDAsInBlcm1pc3Npb25zIjpbImRhc2hib2FyZFwvdXNlcl9tYW5hZ2VtZW50LiU6cnciLCJkYXNoYm9hcmRcL3JvbGVfbWFuYWdlbWVudC4lOnJ3IiwiZGFzaGJvYXJkXC9nYW1lX21hbmFnZW1lbnQuJTpydyJdfQ.kU37gvzfDEqZBw4xN1-IFtcL6VYsK-Rl5o7QNF3lTyU
--- response_body chomp
{"header":{"alg":"HS256","typ":"JWT"},"payload":{"exp":1590502789,"ip":"55.11.22.33","permissions":["dashboard/user_management.%:rw","dashboard/role_management.%:rw","dashboard/game_management.%:rw"],"roles":["admin","custome_service"],"ttl":500,"username":"luffy"},"reason":"everything is awesome~ :p","verified":true}
--- error_code: 200
--- no_error_log
[error]

